﻿/*
 * Copyright (c) 2011 Shawn Rakowski
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to 
 * deal in the Software without restriction, including without limitation the 
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 
 * sell copies of the Software, and to permit persons to whom the Software is 
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in 
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 
 * SOFTWARE. 
 * 
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;
using ROS.Persistence.DAO.DTO;

namespace ROS.Persistence.DAO.MySQLDAO
{
    internal class OrderDAO : MySQLDAO, IOrderDAO
    {
        #region Fields

        private MySQLDAOCmdExecuter<OrderDTO> cmdExec;

        #endregion

        #region Contructor

        public OrderDAO()
        {
            cmdExec = new MySQLDAOCmdExecuter<OrderDTO>(this);
        }

        #endregion

        #region Methods

        private const string insertSQL =
            "INSERT INTO order_tbl (table_number, guest_count, notes, entry_order_event_id) " +
            "VALUES (@table_number, @guest_count, @notes, @entry_order_event_id)";
        public void Insert(OrderDTO dto)
        {
            dto.ID = cmdExec.PerformInsertNonQuery(insertSQL, loadParametersFromDTO(dto));            
        }

        private const string selectByIDSQL =
            "SELECT * FROM order_tbl WHERE order_id = @order_id";
        public OrderDTO SelectByID(long id)
        {
            Dictionary<String, object> parameters = new Dictionary<string, object>();
            parameters["@order_id"] = id;

            MySQLDAOCmdExecuter<OrderDTO>.LoadDTODelegate loadOrderDTO =
                new MySQLDAOCmdExecuter<OrderDTO>.LoadDTODelegate(loadDTOFromReader);
            OrderDTO OrderDTO = 
                (OrderDTO)cmdExec.PerformSingletonSelectQuery(selectByIDSQL, parameters, loadOrderDTO);            

            return OrderDTO;
        }

        const string selectAllSQL = 
            "SELECT * FROM order_tbl";
        public IList<OrderDTO> SelectAll()
        {
            MySQLDAOCmdExecuter<OrderDTO>.LoadDTODelegate loadOrderDTO = 
                new MySQLDAOCmdExecuter<OrderDTO>.LoadDTODelegate(loadDTOFromReader);
            return (IList<OrderDTO>)cmdExec.PerformSelectQuery(selectAllSQL, null, loadOrderDTO);       
        }
      
        private const string updateSQL =
            "UPDATE order_tbl SET table_number = @table_number, guest_count = @guest_count, notes = @notes, " + 
            "entry_order_event_id = @entry_order_event_id, completion_order_event_id = @completion_order_event_id " +
            "WHERE order_id = @order_id";
        public void Update(OrderDTO dto)
        {
            cmdExec.PerformNonQuery(updateSQL, loadParametersFromDTO(dto));
        }

        const string deleteByIDSQL = 
            "DELETE FROM order_tbl WHERE order_id = @order_id";
        public void DeleteByID(long id)
        {
            Dictionary<String, object> parameters = new Dictionary<string, object>();
            parameters["@order_id"] = id;
            cmdExec.PerformNonQuery(deleteByIDSQL, parameters);
        }

        private Dictionary<String, object> loadParametersFromDTO(OrderDTO dto)
        {
            Dictionary<String, object> parameters = new Dictionary<string, object>();
            parameters["@order_id"] = dto.ID;
            parameters["@table_number"] = dto.TableNumber;
            parameters["@guest_count"] = dto.GuestCount;
            parameters["@notes"] = dto.Notes;
            parameters["@entry_order_event_id"] = dto.EntryOrderEventID;
            parameters["@completion_order_event_id"] = dto.CompletionOrderEventID;
            return parameters;
        }

        private OrderDTO loadDTOFromReader(MySqlDataReader dataReader)
        {
            OrderDTO dto = new OrderDTO();
            dto.ID = dataReader.GetInt64("order_id");
            dto.TableNumber = dataReader.GetInt32("table_number");
            dto.GuestCount = dataReader.GetInt32("guest_count");
            dto.Notes = dataReader.GetString("notes");
            dto.EntryOrderEventID = dataReader.GetInt64("entry_order_event_id");
            dto.CompletionOrderEventID = dataReader.GetInt64("completion_order_event_id");
            return dto;
        }

        #endregion
    }
}
